/** 业务逻辑 */
import { getCustomerByUserId } from "@/api/customer";

const business = {
  namespaced: true,
  state: {
    /**
     * 业务逻辑命名方法：  字段名: uni.getStorageSync('__APP__字段名')
     */
    customerInfo: uni.getStorageSync("__APP__customerInfo") || {}, // 客户信息
  },
  actions: {
    // 更新客户信息
    async updateCustomerInfo({ commit, state }, paylod = {}) {
      try {
        const id = paylod?.id || state.customerInfo.userId;
        if (!id) console.error("updateCustomerInfo 用户ID不能为空");
        const pam = { id: id };
        const { data } = await getCustomerByUserId(pam);
        if (data) {
          commit("setCustomerInfo", data);
        }
      } catch (error) {
        console.error(error);
      }
    },
  },
  mutations: {
    /** 客户信息 */
    setCustomerInfo(state, data) {
      state.customerInfo = data;
      uni.setStorageSync("__APP__customerInfo", data);
    },
    onLogout(state) {
      // 退出登陆时，会调用该方法，需要再此处理相关业务数据的，在这写；默认会清除所有业务数据
      console.log("business onLogout");
      const clearKeys = Object.keys(state);
      clearKeys.forEach((key) => {
        const keyName = "__APP__" + key;
        state[keyName] = {};
        uni.removeStorageSync(keyName);
      });
    },
  },
  getters: {},
};
export default business;
